package com.seekting.mynews.view;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.seekting.AppEnv;
import com.seekting.mynews.connect.tiangou.bean.response.GalleryClass;

import java.util.List;

import cn.trinea.android.common.util.ScreenUtils;

/**
 * Created by seekting on 2016/6/28.
 */
public class TitleAdapter extends RecyclerView.Adapter<TitleAdapter.TitleViewHolder> implements View.OnClickListener {
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private List<GalleryClass> mList;
    private final int mPaddingTop;


    private OnRecycleItemClickListener<GalleryClass> mOnItemClickListener;

    public TitleAdapter(Context context, List<GalleryClass> list) {
        this.mContext = context;
        mLayoutInflater = LayoutInflater.from(mContext);
        mPaddingTop = (int) ScreenUtils.dpToPxInt(mContext, 16);
        this.mList = list;
    }

    public void setOnItemClickListener(OnRecycleItemClickListener<GalleryClass> onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    @Override
    public TitleAdapter.TitleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        TextView tv = new TextView(mContext);
        tv.setTextSize(16);
        tv.setPadding(mPaddingTop, mPaddingTop, mPaddingTop, mPaddingTop);
        tv.setGravity(Gravity.CENTER);
        TitleViewHolder titleViewHolder = new TitleViewHolder(tv);
        tv.setOnClickListener(this);
        tv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        return titleViewHolder;
    }

    @Override
    public void onBindViewHolder(TitleAdapter.TitleViewHolder holder, int position) {
        TextView tv = holder.mTextView;
        GalleryClass galleryClass = mList.get(position);
        if (AppEnv.MASK) {
            tv.setText("" + position);
        } else {
            tv.setText(galleryClass.getTitle());
        }
        holder.position = position;
        holder.mGalleryClass = galleryClass;
        tv.setTag(holder);

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            TitleViewHolder titleViewHolder = (TitleViewHolder) v.getTag();
            mOnItemClickListener.onItemClick(titleViewHolder.position, titleViewHolder.mGalleryClass);
        }

    }

    public static class TitleViewHolder extends RecyclerView.ViewHolder {
        TextView mTextView;
        GalleryClass mGalleryClass;
        int position;

        public TitleViewHolder(View itemView) {
            super(itemView);
            mTextView = (TextView) itemView;

        }
    }
}
